package com.gzw.scm.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.gzw.scm.R;
import com.gzw.scm.bean.BWInfo;
import com.gzw.scm.bean.JgInfo;
import com.gzw.scm.utils.T;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2017/5/10.
 */

public class WXAdapter extends BaseAdapter {
    private Context mContext;
    private List<JgInfo> list;
    private static HashMap<Integer, Boolean> isSelected;
    private int selectIndex = -1;//当前item的位置
    private int currentSelection = 0;//偏移量

    public WXAdapter(Context _mContext, List<JgInfo> list) {
        mContext = _mContext;
        this.list = list;
        isSelected = new HashMap<Integer, Boolean>();
        initDate();

    }

    // 初始化isSelected的数据
    private void initDate() {
        for (int i = 0; i < list.size(); i++) {
            getIsSelected().put(i, false);
        }
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        WXAdapter.Holder mHolder = null;
        if (convertView == null) {
            convertView = View
                    .inflate(mContext, R.layout.item_zzxwx, null);
            mHolder = new WXAdapter.Holder(convertView);
            convertView.setTag(mHolder);
        } else {
            mHolder = (WXAdapter.Holder) convertView.getTag();
        }
        JgInfo bwInfo = list.get(position);
        mHolder.tvName.setText(bwInfo.getMc()+"("+bwInfo.getNum()+")");

        mHolder.et_num.setTag(position);
        mHolder.et_num.addTextChangedListener(new MyTextChangeListener(mHolder));
        mHolder.et_num.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean arg1) {
                if(arg1==false){
                    selectIndex = position;
                    currentSelection = ((EditText)view).getSelectionStart();
                }
            }
        });

        final WXAdapter.Holder finalMHolder = mHolder;
        mHolder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                finalMHolder.cb.setSelected(isChecked);
                isSelected.put(position, isChecked);
            }
        });

        if(bwInfo.getNum().equals("0")){
            mHolder.cb.setClickable(false);
            mHolder.cb.setEnabled(false);
            mHolder.et_num.setEnabled(false);
        }

        return convertView;
    }
    static class Holder {
        private TextView tvName;
        private CheckBox cb;
        private EditText et_num;

        public Holder(View view) {
            tvName = (TextView) view.findViewById(R.id.tv_name);
            et_num = (EditText) view.findViewById(R.id.et_num);
            cb = (CheckBox) view.findViewById(R.id.cb);
        }
    }

    public static HashMap<Integer, Boolean> getIsSelected() {
        return isSelected;
    }

    public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {
        WXAdapter.isSelected = isSelected;
    }

    private class MyTextChangeListener implements TextWatcher {

        private Holder holder;

        public MyTextChangeListener(Holder holder) {
            this.holder = holder;
        }

        @Override
        public void afterTextChanged(Editable s) {
            int position = (Integer) holder.et_num.getTag();
            JgInfo jgInfo = list.get(position);
            jgInfo.setNumber(s.toString());
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                                  int count) {
        }
    }

}
